<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
<style>
    .non{
        display: none;
    }
</style>
</head>

<body>
    <input type="text" placeholder="请输入要查询的地区" name="" id="">
    <button>查询</button>
    <table border="1" cellpadding="10px" cellspacing="0" class="non">
        <thead>
            <tr>
                <th>今天</th>
                <th>明天</th>
                <th>后天</th>
            </tr>
        </thead>
        <tbody>

        </tbody>
    </table>



    <script>
        let btn = document.querySelector("button")
        let inp = document.querySelector("input")
        let tbody = document.querySelector("tbody")








        btn.onclick = function () {
            tbody.innerHTML = ""
            let xhr = new XMLHttpRequest()
            xhr.open('get', `https://qw6yvxtrrf.re.qweatherapi.com/geo/v2/city/lookup?location=${inp.value}&key=d763df9f84c543bd868115c8a02bac8b`)
            xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
            xhr.send()
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4) {
                    let data = JSON.parse(xhr.responseText)
                    console.log(data);
                    let id = data.location[0].id
                    console.log(id);
                    d3weather(id)
                    document.querySelector('.non').className = ""


                }
            }
        }
        function d3weather(id) {
            let xhr = new XMLHttpRequest()
            xhr.open('get', `https://qw6yvxtrrf.re.qweatherapi.com/v7/weather/3d?location=${id}&key=d763df9f84c543bd868115c8a02bac8b`)
            xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
            xhr.send()
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4) {
                    let data = JSON.parse(xhr.responseText)
                    console.log(data);
                    let days = data.daily
                    console.log(days);
                    setTable(days)

                }
            }
        }
        function setTable(days) {
            let tr1 = document.createElement('tr')
            let tr2 = document.createElement('tr')
            tbody.appendChild(tr1)
            tbody.appendChild(tr2)
            for (let i in days) {
                let day = days[i].fxDate
                let td = document.createElement('td')
                td.innerHTML = day
                tr1.appendChild(td)
            }
            for (let i in days) {
                let tempmin = days[i].tempMin
                let tempmax = days[i].tempMax
                let td = document.createElement('td')
                td.innerHTML = tempmin + "℃" + "~" + tempmax + "℃"
                tr2.appendChild(td)
            }
        }












    </script>
</body>

</html>